package com.hh.usersystem.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hh.baseframework.util.check.Check;
import com.hh.hibernate.dao.inf.IHibernateDAO;
import com.hh.hibernate.util.dto.HQLParamList;
import com.hh.system.util.StaticVar;
import com.hh.system.util.base.BaseAction;
import com.hh.system.util.model.ReturnModel;
import com.hh.system.util.steady.MsgProperties;
import com.hh.usersystem.bean.usersystem.HHXtZmsx;
import com.hh.usersystem.bean.usersystem.HhXtCd;
import com.hh.usersystem.bean.usersystem.HhXtCz;
import com.hh.usersystem.bean.usersystem.HhXtJs;
import com.hh.usersystem.bean.usersystem.HhXtJsCd;
import com.hh.usersystem.bean.usersystem.HhXtJsCz;
import com.hh.usersystem.bean.usersystem.HhXtYh;
import com.hh.usersystem.bean.usersystem.HhXtYhCdZmtb;
import com.hh.usersystem.bean.usersystem.HhXtYhJs;
import com.hh.usersystem.bean.usersystem.Organization;
import com.hh.usersystem.util.app.LoginUser;
import com.hh.usersystem.util.steady.StaticProperties;
import com.opensymphony.xwork2.ActionContext;

@Service
public class LoginService {
	@Autowired
	private IHibernateDAO<HhXtYhJs> xtyhjsdao;
	@Autowired
	private IHibernateDAO<HhXtYh> xtyhdao;
	@Autowired
	private IHibernateDAO<HhXtJs> xtjsdao;
	@Autowired
	private IHibernateDAO<HhXtJsCd> xtjscddao;
	@Autowired
	private IHibernateDAO<HhXtJsCz> xtjsczdao;
	@Autowired
	private IHibernateDAO<HhXtYhCdZmtb> xtyhcdzmtb;
	@Autowired
	private IHibernateDAO<Organization> hhXtOrgDAO;
	@Autowired
	private UserService userService;
	@Autowired
	private IHibernateDAO<HHXtZmsx> zmsxdao;
	private static final Logger logger = Logger.getLogger(BaseAction.class);

	public ReturnModel savefindLogin(HhXtYh xtYh) {
		ReturnModel returnModel = new ReturnModel();
		List<HhXtYh> xtYhList = xtyhdao.queryList(
				HhXtYh.class,
				new HQLParamList().addCondition(Restrictions.eq("vdlzh",
						xtYh.getVdlzh())));
		if (Check.isEmpty(xtYhList)) {
			returnModel.setType(ReturnModel.TYPE_OK);
			returnModel.setMsg(MsgProperties.system_userlogin_notexist);
		} else {
			if (xtYhList.size() > 1) {
				returnModel.setMsg(MsgProperties.system_userlogin_repeat);
			} else {
				if (LoginUser.loginUserMap.containsKey(xtYhList.get(0).getId())) {
					logger.info("用户已经在线，重新登录！！");
					if (ActionContext.getContext().getSession()
							.get("loginuser") == null) {
						LoginUser.loginUserSession.get(xtYhList.get(0).getId())
								.invalidate();
					}
				}
				if (xtYhList.get(0).getVmm().equals(xtYh.getVmm())) {
					returnModel.setTitleMsg(MsgProperties.system_please_later);
					returnModel.setMsg(MsgProperties.system_is_jump);
					returnModel.setHref("webapp-desktop-desktop");
					HhXtYh hhXtYh = xtYhList.get(0);

					List<HhXtYhJs> hhXtYhJsList = xtyhjsdao.queryList(
							HhXtYhJs.class, new HQLParamList()
									.addCondition(Restrictions.eq("yhId",
											hhXtYh.getId())));

					List<String> jsids = new ArrayList<String>();

					for (HhXtYhJs hhXtYhJs : hhXtYhJsList) {
						jsids.add(hhXtYhJs.getJsId());
					}
					List<HhXtJs> hhXtJsList = new ArrayList<HhXtJs>();
					List<HhXtJsCd> hhXtJsCdList = new ArrayList<HhXtJsCd>();
					List<HhXtJsCz> hhXtJsCzList = new ArrayList<HhXtJsCz>();
					if (jsids.size() != 0) {
						hhXtJsList = xtjsdao.queryList(
								HhXtJs.class,
								new HQLParamList().addCondition(
										Restrictions.eq("nzt", 0l))
										.addCondition(
												Restrictions.in("id", jsids)));
						if (hhXtJsList.size() != 0) {
							hhXtJsCdList = xtjscddao.queryList(HhXtJsCd.class,
									new HQLParamList()
											.addCondition(Restrictions.in(
													"jsId", jsids)));

							hhXtJsCzList = xtjsczdao.queryList(HhXtJsCz.class,
									new HQLParamList()
											.addCondition(Restrictions.in(
													"jsId", jsids)));
						}
					}

					List<HhXtCd> hhXtCdList = new ArrayList<HhXtCd>();
					List<HhXtCz> hhXtCzList = new ArrayList<HhXtCz>();
					List<HhXtCd> hhXtCdZmtbList = new ArrayList<HhXtCd>();

					List<String> hhxtcdIdList = new ArrayList<String>();

					for (HhXtJsCd hhXtJsCd : hhXtJsCdList) {
						hhXtCdList.add(hhXtJsCd.getHhXtCd());
						hhxtcdIdList.add(hhXtJsCd.getHhXtCd().getId());
					}
					for (HhXtJsCz hhXtJsCz : hhXtJsCzList) {
						HhXtCz hhXtCz = hhXtJsCz.getHhXtCz();
						if (Check.isEmpty(hhXtCz.getOperLevel())) {
							hhXtCz.setOperLevel(hhXtJsCz.getOperLevel());
						} else {
							if (StaticProperties.findOperationLevel(hhXtJsCz
									.getOperLevel()) > StaticProperties
									.findOperationLevel(hhXtCz.getOperLevel())) {
								hhXtCz.setOperLevel(hhXtJsCz.getOperLevel());
							}
						}

						hhXtCzList.add(hhXtCz);
					}

					hhXtCdZmtbList = queryZmtbList(hhXtYh, hhxtcdIdList);

					hhXtYh.setHhXtCdList(hhXtCdList);
					hhXtYh.setHhXtCzList(hhXtCzList);
					hhXtYh.setHhXtYhCdZmtbList(hhXtCdZmtbList);

					hhXtYh.setHhXtJsList(hhXtJsList);
					// 岗位
					userService.editHhXtYh_orgList(hhXtYh);

					for (int i = 0; i < hhXtYh.getOrganizationList().size(); i++) {
						List<Organization> organizations1 = hhXtOrgDAO
								.queryList(
										"select o from Organization o where ? like '%'|| o.code_||'%' and length(o.code_) in (select max(length(o.code_)) from Organization o where ? like '%'|| o.code_||'%' group by lx_) ",
										new Object[] {
												hhXtYh.getOrganizationList()
														.get(i).getCode_(),
												hhXtYh.getOrganizationList()
														.get(i).getCode_() });
						for (Organization organization : organizations1) {
							if (organization.getLx_() == 0) {
								hhXtYh.getOrganizationList().get(i)
										.setJt(organization);
							} else if (organization.getLx_() == 1) {
								hhXtYh.getOrganizationList().get(i)
										.setJg(organization);
							} else if (organization.getLx_() == 2) {
								hhXtYh.getOrganizationList().get(i)
										.setBm(organization);
							}
						}
					}

					createZmsx(hhXtYh);

					List<Organization> organizations = hhXtYh
							.getOrganizationList();

					if (organizations.size() == 1) {
						ActionContext
								.getContext()
								.getSession()
								.put("currOrg",
										hhXtYh.getOrganizationList().get(0));
					} else {
						ActionContext.getContext().getSession()
								.put("currOrg", hhXtYh.getOrganizationList());
					}

					if (!Check.isEmpty(hhXtYh.getHhXtZmsx().getDefaultOrgId())) {
						for (Organization organization : organizations) {
							if (hhXtYh.getHhXtZmsx().getDefaultOrgId()
									.equals(organization.getId())) {
								ActionContext.getContext().getSession()
										.put("currOrg", organization);
							}
						}
					}
					HhXtYh hhXtYh2=new HhXtYh();
					try {
						BeanUtils.copyProperties(hhXtYh2, hhXtYh);
						hhXtYh2.setVmm("");
						ActionContext.getContext().getSession()
						.put("loginuser", hhXtYh2);
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					}
				} else {
					returnModel
							.setMsg(MsgProperties.system_userlogin_wrong_password);
				}
			}
		}
		return returnModel;
	}

	private void createZmsx(HhXtYh hhXtYh) {
		if (hhXtYh.getHhXtZmsx() == null) {
			HHXtZmsx hhXtZmsx = new HHXtZmsx();
			hhXtZmsx.setId(UUID.randomUUID().toString());
			hhXtYh.setHhXtZmsx(hhXtZmsx);
			xtyhdao.updateEntity(hhXtYh);
		}
	}

	private List<HhXtCd> queryZmtbList(HhXtYh hhXtYh, List<String> hhxtcdIdList) {
		List<HhXtYhCdZmtb> hhXtYhCdZmtbList = xtyhcdzmtb.queryList(
				HhXtYhCdZmtb.class,
				new HQLParamList().addCondition(
						Restrictions.eq("yhId", hhXtYh.getId())).addCondition(
						Order.asc(StaticVar.ORDER)));

		List<HhXtCd> hhXtCdZmtbList = new ArrayList<HhXtCd>();
		for (HhXtYhCdZmtb hhXtYhCdZmtb : hhXtYhCdZmtbList) {
			if (hhXtYhCdZmtb.getHhXtCd() != null) {
				if (hhxtcdIdList.contains(hhXtYhCdZmtb.getHhXtCd().getId())) {
					hhXtCdZmtbList.add(hhXtYhCdZmtb.getHhXtCd());
				}

			}
		}

		return hhXtCdZmtbList;
	}
}
